
#ifndef __JPG_LOADER_H__
#define __JPG_LOADER_H__
#include "CommonUtility.h"
using namespace Mz_CommonUtility;
#include "imaging.h"
#include "JPGApi.h"
#include "Helper.h"

class JPGLoader
{
public:
	JPGLoader();
	virtual ~JPGLoader();

	HRESULT LoadImage( const TCHAR *fileName );
	HRESULT InitDecode();
	HRESULT Decode( const TCHAR *fileName, RECT *dstRect, int rotateDegree = 0, RECT *srcRect  = NULL  );
	HRESULT Decode( HDC hdc, const TCHAR *fileName, RECT *dstRect, int rotateDegree = 0, RECT *srcRect  = NULL, BOOL bDecodeOriginal = FALSE );
	HRESULT Draw( HDC hdc );
	HRESULT GetImageInfo( ImageInfo *imageInfo );
	HRESULT ReadHeader();
	void Release();
private:
	void initDecodeParam(void);
	void *SsbSipJPEGDecodeInit(void);
	JPEG_ERRORTYPE SsbSipJPEGDecodeExe(void *openHandle);
	char *SsbSipJPEGGetDecodeInBuf(void *openHandle, long size);
	char *SsbSipJPEGGetDecodeOutBuf (void *openHandle, UINT32 *size);	
	JPEG_ERRORTYPE SsbSipJPEGGetConfig (JPEGConf type, INT32 *value);
	JPEG_ERRORTYPE SsbSipJPEGDecodeDeInit (void *openHandle);
	char *SsbSipJPEGGetDecodeOutPhyBuf(void *openHandle);
	char *SsbSipJPEGGetRGBPhyBuf(void *openHandle, INT32 width, INT32 height);
	char *SsbSipJPEGGetRGBBuf(void *openHandle, INT32 width, INT32 height);
	BOOL ConvertYCBYCRToRGB(int inBuf, INT32 srcwidth, INT32 srcheight, 
		unsigned long srcType,
		int outBuf, INT32 dstwidth, INT32 dstheight,
		unsigned long dstType, INT32 srcOffsetX = 0, INT32 srcOffsetY = 0 );
	char *SsbSipJPEGGetRGBRotatePhyBuf( void *openHandle, INT32 width, INT32 height);
	char *SsbSipJPEGGetRGBRotateBuf( void *openHandle, INT32 width, INT32 height);
	BOOL RotateJPG( int inBuf, INT32 srcwidth, INT32 srcheight, unsigned long srcType, int outBuf, int rotateDegree  );
	char *SsbSipJPEGGetOriginalRGBPhyBuf( void *openHandle, INT32 width, INT32 height );
	char *SsbSipJPEGGetOriginalRGBBuf( void *openHandle, INT32 width, INT32 height );
	char *SsbSipJPEGGetOriginalRGBRotatePhyBuf( void *openHandle, INT32 width, INT32 height );
	char *SsbSipJPEGGetOriginalRGBRotateBuf( void *openHandle, INT32 width, INT32 height );

	
public:
	int m_imageWidth;
	int m_imageHeight;
	char *m_imageData;
	IStream *m_pStream;
	BOOL m_bHeaderRead;
	int m_numOfComponents;
	JPG_CTX *m_jCtx;
	CRect m_dstRect;
	CRect m_srcRect;
	int m_depth;
	int m_lineStride;
	void* m_handle;
	const TCHAR *m_fileName;
	int m_rotateDegree;

};
#endif


